package com.jo.helloword;

import com.jo.Config;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author Jo
 * @date 2017/12/6
 */
public class Producer {
    public static void main(String[] args) {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(Config.host);
        connectionFactory.setPort(Config.port);
        try {
            Connection connection = connectionFactory.newConnection();
            Channel channel = connection.createChannel();
            //负载均衡
            channel.basicQos(1);
            channel.queueDeclare("test_queue", false, false, false, null);
            System.out.println("生产者已成功开启");
            //第一个参数空字符串，代表使用默认的匿名交换器
            channel.basicPublish("", "test_queue", null, "test message".getBytes("UTF-8"));
            System.out.println("生产者已生产");
            channel.close();
            connection.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }
}
